#include "luaimage.h"   
#include "resmanager.h"
#include "sdl/SDL_opengl.h"
extern ResManager myResManager;



int pot(int x) {
       int val = 1;
       while (val < x) {
               val *= 2;
       }
       return val;
}
int LUAIMAGE_load(string resroot,lua_State *L) /**< id,respath \return w,h */ {
	if(!(API_ARGCHECK(IS_INTNUMBER,L,ARG2_1) && API_ARGCHECK(IS_STRING,L,ARG2_2))) {
		E("LUAIMAGE_load: Invalid arguments. Expected int,string [ id, respath ]")
		return 0;
	}
	int resid = (int)lua_tonumber(L,ARG2_1);
	const char* s_respath = lua_tostring(L,ARG2_2);
	if(s_respath == NULL) {
		E("ResManager:LUAIMAGE_load: Invalid respath")
		return 0;
	}
	if(resid < 0 || strlen(s_respath) == 0) {
		E("ResManager:LUAIMAGE_load: Invalid data for resid or respath")
		return 0;
	}

	int s_len = strlen(s_respath);
	for(int i = 0;i < s_len-4;++i) {
		if((s_respath[i] == '/' || s_respath[i] == '\\') 
			&& s_respath[i+1] == '.' && s_respath[i+2] == '.' && 
			(s_respath[i+3] == '/' || s_respath[i+3] == '\\')) {
			E("ResManager:LUAIMAGE_load: invalid characters '..' in the resource path. Please refer without using them")
			return 0;
		}
	}
	string respath = string(s_respath);
		
	
	string fullpath = resroot + 
						#if defined(WIN32) || defined(_WIN32)
							string("\\")
						#else
							string("/")
						#endif
						+ respath;
	

	SDL_Surface * texture_imageraw = NULL;
	texture_imageraw = IMG_Load(fullpath.c_str());
	SDL_Surface * texture_converted = 	SDL_CreateRGBSurface(0,texture_imageraw->w,texture_imageraw->h, 24, 0x0000FF, 0x00FF00, 0xFF0000, 0x000000);
	SDL_BlitSurface(texture_imageraw, NULL, texture_converted, NULL);	
	
	if (texture_converted == NULL || texture_imageraw == NULL) {
		E("LUAIMAGE_load: Unable to load " << fullpath << " Error: " << IMG_GetError())
		return 0;
	}

//	M("LUAIMAGE_load: Texture params: w="  << texture->w << " h=" << texture->h << " tex bytes/pixel=" << texture->format->BytesPerPixel << " pixel=" << texture->pixels)	
//	M("LUAIMAGE_load: Shift RGB : " << texture->format->Rshift << " " << texture->format->Gshift << " " << texture->format->Bshift << " " )
//	GLuint resida[1];
//    glGenTextures(1, &resida[0]); 
//    M("genresid = " << resida[0])
//    glBindTexture(GL_TEXTURE_2D, resida[0]);
//	resid = resida[0];
    
    int width = pot(texture_converted->w);
    int height = pot(texture_converted->h);
    
	GLuint textureID;
	glGenTextures(1, &textureID);
	glBindTexture(GL_TEXTURE_2D, textureID);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL);
	glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, texture_converted->w, texture_converted->h, GL_RGB, GL_UNSIGNED_BYTE, texture_converted->pixels);
//	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//	glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

	resid = textureID;
	

	/*
    if (texture->format->BytesPerPixel == 4) {
    	M("4 bytes/pixel")
       glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, texture->w,texture->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture->pixels );
    }
    else {
    	M("other bytes/pixel")
       glTexImage2D(GL_TEXTURE_2D, 0, 4, texture->w,texture->h, 0, GL_RGB, GL_UNSIGNED_BYTE, texture->pixels);
    }
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);*/
        //if (SDL_MUSTLOCK(texture)) SDL_UnlockSurface(texture);
    SDL_FreeSurface(texture_imageraw);
    SDL_FreeSurface(texture_converted);
    lua_pushnumber(L,textureID);
    lua_pushnumber(L,width);
    lua_pushnumber(L,height);
	return 3;
}
